﻿using System;
using System.Data;
using System.Web.UI;
using VitCode.Web.UI;

namespace CRS.Web.BaseData
{
    public partial class OrdersList : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            
            pgList.DataSourceDelegate = BindData;
            if (!IsPostBack)
            {
                try
                {
                    Session["OrderSource"] = null;
                    Session["SelectOrder"] = null;
                    BindDropDownList();
                    pgList.BindGrid();
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
        }

        private DataTable DataSource
        {
            get
            {
                if (Session["OrderSource"] == null)
                {
                    Session["OrderSource"] = InitData();
                }
                return (DataTable)Session["OrderSource"];
            }
        }

        private object BindData()
        {
            return DataSource;
        }

        private object InitData()
        {
            var table = new DataTable();
            table.Columns.Add("单号");
            table.Columns.Add("创建人");
            table.Columns.Add("创建时间");
            table.Columns.Add("状态");
            table.Columns.Add("备注");
            var date = DateTime.Now;
            for (int i = 0; i < 200; i++)
            {
                DataRow row = table.NewRow();
                row["单号"] = "VC_" + i.ToString().PadLeft(10, '0');
                row["创建人"] = "张三" + i.ToString();
                row["创建时间"] = date.AddMinutes(i*1.11).ToString("yyyy-MM-dd HH:mm:ss");
                row["状态"] = "新建";
                row["备注"] = "Test It's OK!";
                table.Rows.Add(row);
            }
            return table;
        }

        private void BindDropDownList()
        {
            
        }

        protected void pgList_OnGridItemSelect(object sender, PaginationGridItemSelectArgs e)
        {
            string sss = pgList.SelectedDataKeyValue;
            var selectInfo = DataSource.Select("单号='" + pgList.SelectedDataKeyValue + "'");
            Session["SelectOrder"] = selectInfo;
            Response.Redirect("OrdersEdit.aspx?billNo=" + sss);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("OrdersEdit.aspx");
        }
    }
}
